<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Cadastrar extends TPage
{
    public function onInit($param)
    {        
        parent::onInit($param);

        $this->artigoList->DataTextField='Titulo';
		$this->artigoList->DataValueField='ArtigoID';
		$this->artigoList->DataSource=ArtigoRecord::finder()->findAll();
		$this->artigoList->dataBind();

        $this->avaliadorList->DataTextField='Nome';
		$this->avaliadorList->DataValueField='UsuarioID';
		$this->avaliadorList->DataSource=UserRecord::finder()->findAll();
		$this->avaliadorList->dataBind();
    }    
    
    public function criarAvaliacao($sender,$param)
	{
        if($this->IsValid)  // when all validations succeed
        {
            // populates a avaliacaoRecord object with user inputs
            $avaliacaoRecord                = new AvaliacaoRecord; 
            $avaliacaoRecord->Status        = "0";
            $avaliacaoRecord->AvaliadorID   = $this->avaliadorList->getSelectedValue();
            $avaliacaoRecord->ArtigoID      = $this->artigoList->getSelectedValue();
 
            // saves to the database via Active Record mechanism
            $avaliacaoRecord->save();
 
            // redirects the browser to the homepage
            $this->Response->redirect("./index.php?page=Avaliacao.Listar&all=1&status=0");
        }
    }

    public function validaAvaliador($sender,$param)
    {
        // Construts a query criteria
        $criteria                          = new TActiveRecordCriteria;
        $criteria->Condition = 'AutorID = '.$this->avaliadorList->getSelectedValue().' and ArtigoID = '.$this->artigoList->getSelectedValue();
        $artigoAutorRecord = ArtigoAutorRecord::finder()->findAll($criteria);

        if(count($artigoAutorRecord) == 0)
            $param->IsValid = true;
        else
            $param->IsValid = false;
    }
}
?>
